package comp.config.ext.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.justep.cloud.microservice.http.MicroServiceBaseController;

import comp.config.entity.Config;
import comp.config.service.ConfigService;


@RestController
@RequestMapping("/config/config/")
public class ConfigController extends MicroServiceBaseController {
	
	@Autowired
	private ConfigService configUserService;
	
	private HttpHeaders defaultHttpHeaders() {
		HttpHeaders headers = new HttpHeaders();
		headers.putAll(getSystemHeader());
		processContentType(headers);
		return headers;
	}
	
	/** 
	 查询时候注意 secure为1的配置为安全配置 只能内网访问   
	    内网api调用的header中
	    X-Credential-User-Id:x-credential-client-id
	    	这种内网api调用返回secret
	    	否则注意secret的值 擦除为 星
	*/
	
	//这里提供增删改查api 不一一枚举 参考spring data rest api  post新增的时候 id可以通过前端指定
	@RequestMapping(path="{id}",method=RequestMethod.GET)
	public ResponseEntity<Config> getConfig(@PathVariable String id) {
		Config config = configUserService.getConfig(id);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	/**
	 * <p>配置新增</p>
	 * <p>gox5编译生成的实体主键ID由uuid生成，POST新增数据id不可以通过前端指定</p>
	 * @param configs
	 * @return
	 */
	@GetMapping("config")
	public ResponseEntity<List<Config>> getConfig() {
		List<Config> configs = configUserService.findAll();
		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.CREATED);
	}
	
	/**
	 * <p>配置新增</p>
	 * <p>gox5编译生成的实体主键ID由uuid生成，POST新增数据id不可以通过前端指定</p>
	 * @param configs
	 * @return
	 */
	@PostMapping("config")
	public ResponseEntity<List<Config>> addConfig(@RequestBody List<Config> configs) throws RuntimeException {
		configs = configUserService.addConfig(configs);
		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.CREATED);
	}
	
	/**
	 * <p>配置修改更新</p>
	 * @param configs
	 * @return
	 */
//	@PutMapping("config")
//	public ResponseEntity<List<Config>> modifyConfig(@RequestBody List<Config> configs) throws Exception {
//		configs = configUserService.addConfig(configs);
//		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.OK);
//	}
	
	/**
	 * <p>删除配置</p>
	 * @param id
	 */
	@DeleteMapping("{id}")
	public void removeConfig(@PathVariable String id) {
		configUserService.removeById(id);
	}
	
	@DeleteMapping("/group/{group}")
	public void removeConfigByGroup(@PathVariable String group) {
		configUserService.removeByGroup(group);
	}
	
	//value字段作为 数据 追加一项 返回修改后的config对象
	@RequestMapping(path="/append/{id}",method=RequestMethod.PUT)
	public ResponseEntity<Config> apppendValue(@PathVariable String id
			,@RequestBody JSONObject value) {
		Config config = configUserService.appendValue(id,value);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	@RequestMapping(path="/{id}/{valueId}",method=RequestMethod.DELETE)
	public ResponseEntity<Config> removeValue(@PathVariable String id,@PathVariable String valueId) {
		Config config = configUserService.removeValue(id,valueId);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	/**
	  首先根据key值来查找记录
	  如果存在  
	 	根据记录的value确定要形成object的keys
	如果不存在
	      利用like'{key}%'获取keys列表形成list返回 记得带id 修改写回的时候 根据id更新 	 
	 */
	@RequestMapping(path="configs/search/like",method=RequestMethod.GET)
	public ResponseEntity<List<Config>> getConfigsByKey(@RequestParam String key
			,@RequestParam(defaultValue="",required=false) String group
			,@RequestHeader(name="X-Credential-User-Id",defaultValue="",required=false) String credential){
		List<Config> configs = configUserService.getConfigsByKey(key,group,credential);
		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	@RequestMapping(path="search",method=RequestMethod.GET)
	public ResponseEntity<Config> getConfigByKey(@RequestParam String key,@RequestParam(defaultValue="",required=false) String group
			,@RequestHeader(name="X-Credential-User-Id",defaultValue="",required=false) String credential) {
		Config config = configUserService.getConfigByKey(key,group,credential);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	/**
	 * 小程序设计时需要查询小程序配置了多少组参数，然后让用户选择使用哪一组的参数
	 * 该api直接通过com.qq.weixin.miniapp.appID查询即可知道有多少分组
	 * @param key
	 * @return
	 */
	@GetMapping(path="groups")
	public ResponseEntity<List<Config>> getConfigsByKey(@RequestParam(name="key",defaultValue="com.qq.weixin.miniapp.appID")String key) {
		List<Config> configs = configUserService.getConfigsByKey(key);
		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	/**
	 * 根据appid查询配置信息，改接口主要是小程序使用
	 * @param appid
	 * @param credential
	 * @return
	 */
	@RequestMapping(path="configs/search/appid/{appid}",method=RequestMethod.GET)
	public ResponseEntity<List<Config>> getConfigsByAppid(@PathVariable String appid
			,@RequestHeader(name="X-Credential-User-Id",defaultValue="",required=false) String credential) {
		List<Config> configs = configUserService.getConfigsByAppid(appid,credential);
		return new ResponseEntity<List<Config>>(configs, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	@PostMapping(path="appendItem")
	public ResponseEntity<Config> appendItem(String key,@RequestBody JSONObject jsonObject) {
		Config config = configUserService.appendItem(key,jsonObject);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	@DeleteMapping(path="removeItem")
	public ResponseEntity<Config> removeItem(String key,String valueId) {
		Config config = configUserService.removeItem(key,valueId);
		return new ResponseEntity<Config>(config, defaultHttpHeaders(), HttpStatus.OK);
	}
	
	@Override
	protected Map<String, Map<String, Integer>> getExceptionStatusMap() {
		return null;
	}
}
